﻿using System;
using BarManager.Core.ApplicationService.Interfaces;
using BarManager.UI.Web.Models;
using System.Web.Mvc;

namespace BarManager.UI.Web.Controllers
{
    public class AccountController : Controller
    {
        IAuthenticationService authProvider;

        public AccountController(IAuthenticationService authProvider)
        {
            this.authProvider = authProvider;
        }

        [HttpGet]
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(LoginViewModel login, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                bool result = authProvider.Authenticate(login.Usuario, login.Password);                
                if(result)
                {
                if(login.Usuario == "admin")
                    return Redirect(returnUrl ?? Url.Action("List", "Orden")); 
                else
                    return Redirect(returnUrl ?? Url.Action("List", "OrdenMozo")); 
                }                 
                else
                    ModelState.AddModelError("", "Usuario o password inválido");
            }
            return View();
        }

        public ActionResult Logout()
        {
            authProvider.Logout();
            return RedirectToAction("Login");
        }
    }
}
